package com.lill.product.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.model.P;
import com.lill.common.component.util.IdUtil;
import com.lill.org.model.CompanyBranchModel;
import com.lill.org.service.ICompanyBranchService;
import com.lill.product.dao.IBaseDiscountDao;
import com.lill.product.model.BaseDiscountModel;
import com.lill.product.service.IBaseDiscountService;

/**
 * 厂家返利Service实现类
 *
 * @author lill
 * @date 2021-08-31 11:01:26
 */
@Service("com.lill.product.service.impl.BaseDiscountServiceImpl")
public class BaseDiscountServiceImpl implements IBaseDiscountService {
	
	private final static String prefix="discount";
	
    @Autowired
    private IBaseDiscountDao baseDiscountDao;
    @Autowired
    private ICompanyBranchService branchService;

    public BaseDiscountModel queryObject(String id) {
        return baseDiscountDao.queryObject(id);
    }

    public List<BaseDiscountModel> queryList(Map<String, Object> map) {
    	List<BaseDiscountModel> discountList=baseDiscountDao.queryList(map);
    	if(CollectionUtils.isNotEmpty(discountList)){
    		Map<String, String> branchMap=new HashMap<String, String>();
    		for (BaseDiscountModel discountModel : discountList) {
				if(branchMap.containsKey(discountModel.getBranchId())){
					discountModel.setBranchName(branchMap.get(discountModel.getBranchId()));
				}else{
					CompanyBranchModel branchModel=branchService.queryObject(discountModel.getBranchId());
					if(null!=branchModel){
						discountModel.setBranchName(branchModel.getName());
					}
				}
			}
    	}
        return discountList;
    }

    public int queryTotal(Map<String, Object> map) {
        return baseDiscountDao.queryTotal(map);
    }

    public int save(BaseDiscountModel baseDiscount) {
    	Set<Integer> mapNameSet=new HashSet<Integer>();
        Map<String, Object> queryMap=new HashMap<String, Object>();
        queryMap.put("companyId", baseDiscount.getCompanyId());
        queryMap.put("branchIdIsNull", "isNull");
        List<BaseDiscountModel> companyDataList=queryList(queryMap);
        if(CollectionUtils.isNotEmpty(companyDataList)){
        	for (BaseDiscountModel discountVo : companyDataList) {
        		mapNameSet.add(Integer.parseInt(discountVo.getMapName().replace(prefix, "")));
			}
        }
    	if(StringUtils.isNotEmpty(baseDiscount.getBranchId())){
    		queryMap.remove("branchIdIsNull");
    		queryMap.put("branchId", baseDiscount.getBranchId());
    		List<BaseDiscountModel> branchDataList=queryList(queryMap);
    		if(CollectionUtils.isNotEmpty(branchDataList)){
    			for (BaseDiscountModel discountVo : branchDataList) {
            		mapNameSet.add(Integer.parseInt(discountVo.getMapName().replace(prefix, "")));
    			}
            }
    	}
    	int suffix=0;
    	if(mapNameSet.isEmpty()){
    		suffix=1;
    	}else{
    		for (int i = 1; i < mapNameSet.size(); i++) {
				if(!mapNameSet.contains(i)){
					suffix=i;
				}
			}
    		if(0==suffix){
    			suffix=mapNameSet.size()+1;
    		}
    	}
    	baseDiscount.setMapName(prefix+suffix);
    	baseDiscount.setId(IdUtil.createIdbyUUID());
        return baseDiscountDao.save(baseDiscount);
    }

    public int update(BaseDiscountModel baseDiscount) {
        return baseDiscountDao.update(baseDiscount);
    }

	public P deleteLogicByMap(Map<String, Object> map) {
		baseDiscountDao.deleteLogicByMap(map);
		return P.fail();
	}
}
